<template>
  <div
    class="put-launch"
    :style="{ left: !isShrink ? '487px' : 0 }"
    @click="isShrink = !isShrink"
  >
    <img
      src="@/assets/images/put-launch.png"
      :style="{ transform: `rotate(${isShrink ? '-180deg' : '0'})` }"
      alt=""
    />
  </div>
  <transition name="collapse-fade-absolute-in">
    <div v-show="!isShrink" class="search-collapse">
      <el-scrollbar>
        <div class="collapse-title">预警信息汇总</div>
        <div class="select-warp">
          <el-select
            class="selectrest"
            v-model="cityType"
            :placeholder="'行政级别'"
            @change="cityTypeChange()"
          >
            <el-option
              v-for="(item, k) in cityList"
              :value="item.code"
              :label="item.name"
              :key="k"
            ></el-option>
          </el-select>
          <el-select
            class="selectrest"
            v-model="earlyType"
            placeholder="预警类型"
            @change="changeearlyType()"
          >
            <el-option
              v-for="(item, k) in earlytypeList"
              :value="item.code"
              :label="item.name"
              :key="k"
            ></el-option>
          </el-select>
        </div>

        <div class="collapse-type">
          <div
            :class="['warningList', `warning${item.type}`]"
            v-for="(item, key, i) in warningStore.statisticsData"
            :key="i"
            @click="fliterwarningfortype(item)"
          >
            <div class="name">{{ item.name }}预警</div>
            <div class="num">{{ item.num }}</div>
            <div :class="['numbg', `num${item.type}`]"></div>
          </div>
        </div>
        <div
          class="collapse-list"
          v-if="warningStore.warningInfoData.length > 0"
        >
          <div
            class="warningAreaList"
            v-for="(w, i) in warningStore.warningInfoData"
            :key="i"
          >
            <div class="dotline">
              <!-- 竖线 -->
              <div class="line"></div>
              <!-- 圆点 -->
              <div class="dot"></div>
              <!-- 时间戳 -->
            </div>
            <div class="event_box">
              <div class="time">{{ w.createTime }}</div>
              <div class="content">
                <div class="address">
                  <img src="@/assets/images/locate.png" />{{
                    w.effectiveAreaName
                  }}
                </div>
                <div class="bottom">
                  <div class="early">
                    <img
                      :src="`/guotuicon/${w.warnLevel}_${w.eventTypeCode}.jpg`"
                      class="guoiconImg"
                      alt=""
                      srcset=""
                    />
                    <!-- <p class="warningcont" :style="{color:w.severity}">{{w.sendTimeStr}}{{ w.headline }}</p> -->
                    <p :class="['warningcont', `warning${w.warnLevel}`]">
                      {{ w.createTime }}{{ w.headline }}
                    </p>
                  </div>
                  <div class="prompt">{{ w.warnDescription }}</div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="collapse-list" v-else>
          <el-empty description="当前无预警" />
        </div>
        <div class="downloadbox">
          <div class="title">文档下载</div>
          <div>
            <el-date-picker
              v-model="downloadtime"
              prefix-icon=""
              type="datetimerange"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
            <div class="listbox" v-if="isWarningDown">
              <img src="@/assets/images/downicon.png" class="imgicon" />
              重要天气预24小时预警产品文档
              <span class="listdown" @click="downloadfile">立即下载</span>
            </div>
          </div>
        </div>
      </el-scrollbar>
    </div>
  </transition>
</template>

<script setup lang="ts">
import dayjs from 'dayjs'
import apis from '../api/api'
import { useWarningStore } from '@/pages/warning/store/store'
import { color } from 'echarts/core'
const warningStore = useWarningStore()
let timer = ref(null)
const permissions = localStorage.getItem('userInfo')
const isWarningDown = computed(() => {
  return (
    permissions.includes('warning:download') || permissions.includes('*:*:*')
  )
})
const isShrink = inject('isShrink')
const earlytypeList = ref([{ name: '全部', code: '' }])
const cityList = ref([
  { name: '全部', code: 0 },
  { name: '省', code: 1 },
  { name: '市', code: 2 },
  { name: '区县', code: 3 }
])
const cityType = ref()
const earlyType = ref()
const colortype = ref('')
const warnInfoData = ref([])
const downloadtime = ref([
  dayjs().subtract(1, 'd').format('YYYY-MM-DD 00:00:00'),
  dayjs().subtract(1, 'd').format('YYYY-MM-DD 23:59:59')
])
const getearlyListData = () => {
  earlytypeList.value = []
  apis.getTypeList({ administrationLevel: cityType.value }).then(res => {
    if (res.data) {
      earlytypeList.value.push(...res.data)
    }
  })
}
const changeearlyType = async () => {
  warningStore.warnType = earlyType.value
  await warningStore.getWarningStatistics()
  await warningStore.getWarningInfoData()
  timer.value = setInterval(() => {
    warningStore.getWarningStatistics()
    warningStore.getWarningInfoData()
  }, 10 * 60 * 1000)
  warnInfoData.value = warningStore.warningInfoData
}
const cityTypeChange = async () => {
  warningStore.level = cityType.value
  await getearlyListData()
  await changeearlyType()
}
const downloadfile = () => {
  //const res= await download({fileUrl:filePath})
  apis
    .exportWarningPDF({
      startTime: downloadtime.value[0],
      endTime: downloadtime.value[1]
    })
    .then(res => {
      const link = document.createElement('a')
      let blob = new Blob([res], { type: 'application/pdf' })
      let objectUrl = URL.createObjectURL(blob) // 创建URL

      link.href = objectUrl
      //⾃定义⽂件名
      link.download = '重要天气预24小时预警产品文档' //
      //下载⽂件
      link.click() //
      //释放内存
      URL.revokeObjectURL(objectUrl) //
    })
}
const fliterwarningfortype = item => {
  colortype.value = colortype.value == item.type ? '' : item.type
  if (colortype.value != '') {
    warningStore.warningInfoData = warnInfoData.value.filter(
      el => colortype.value == el.warnLevel
    )
  } else {
    warningStore.warningInfoData = warnInfoData.value
  }
}
onMounted(async () => {
  await getearlyListData()
  await changeearlyType()
})
onUnmounted(() => {
  clearInterval(timer.value)
  timer.value = null
})
</script>

<style scoped lang="scss">
.put-launch {
  width: 30px;
  height: 50px;
  background: rgba(0, 17, 46, 0.6);
  box-shadow: 0px 0px 0px 1px rgba(0, 204, 255, 0.05),
    inset 0px 0px 14px 1px #00ccff;
  border-radius: 0px 10px 10px 0px;
  border: 1px solid #00a8ff;
  position: fixed;
  top: 50%;
  transform: translateY(-50%);
  z-index: 1000000;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: left 0.5s ease;
  img {
    width: 21px;
    height: 15px;
  }
}

.search-collapse {
  position: absolute;
  left: 0;
  top: 29px;
  width: 487px;
  text-align: center;
  height: calc(100% - 29px);
  background: rgba(0, 17, 46, 0.6);
  box-shadow: 0px 0px 0px 1px rgba(0, 204, 255, 0.05),
    inset 0px 0px 14px 1px #00ccff;
  border: 1px solid #00a8ff;
  z-index: 1200;
  .select-warp {
    display: flex;
    padding: 0 10px;
    box-sizing: border-box;
    :deep(.el-select) {
      margin-right: 5px;
    }
  }
  :deep(.selectrest) {
    width: 400px;
    margin-top: 16px;
    .el-select__wrapper {
      height: 38px;
      background: rgba(2, 19, 43, 0.69);
      box-shadow: inset 0px 0px 6px 1px #00b9ff;
      border-radius: 4px 4px 4px 4px;
      border: 1px solid rgba(34, 249, 255, 0.23);
      .el-select__selected-item {
        color: #fff;
      }
    }
  }

  .collapse-title {
    width: 367px;
    height: 41px;
    line-height: 41px;
    font-size: 16px;
    color: #ffffff;
    margin: 19px auto 0;
    text-align: center;
    background: url(@/assets/images/warningtitle.png) no-repeat center;
  }

  .collapse-type {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    margin: 21px 0;
    box-sizing: border-box;
    .warningList {
      width: 113px;
      height: 138px;
      padding: 16px 28px;
      box-sizing: border-box;
      color: #ffffff;
      cursor: pointer;
      &.warningBlue {
        background: url('@/assets/images/bluebg.png') no-repeat center/100% 100%;
      }
      &.warningYellow {
        background: url('@/assets/images/yellowbg.png') no-repeat center/100%
          100%;
      }
      &.warningOrange {
        background: url('@/assets/images/orangebg.png') no-repeat center/100%
          100%;
      }
      &.warningRed {
        background: url('@/assets/images/redbg.png') no-repeat center/100% 100%;
      }
      .name {
        font-size: 14px;
        margin-bottom: 28px;
      }
      .num {
        font-size: 30px;
        font-family: Impact;
      }
      .numbg {
        width: 57px;
        height: 69px;
        margin-top: -30px;
        //animation: flicker 1s infinite; /* 设置动画为2秒钟的时间，并无限循环 */
        &.numBlue {
          background: url('@/assets/images/blue.png') no-repeat center/100% 100%;
        }
        &.numYellow {
          background: url('@/assets/images/yellow.png') no-repeat center/100%
            100%;
        }
        &.numOrange {
          background: url('@/assets/images/orange.png') no-repeat center/100%
            100%;
        }
        &.numRed {
          background: url('@/assets/images/red.png') no-repeat center/100% 100%;
        }
      }
    }
  }

  .collapse-list {
    height: 358px;
    overflow: scroll;
    .warningAreaList {
      display: flex;
      padding: 0 30px;
      .dotline {
        width: 30px;
        margin-right: 25px;
        position: relative;
      }
      /*竖线*/
      .dotline .line {
        width: 2px;
        height: 100%;
        background: #00acff;
        position: absolute;
        top: 0;
        left: 11px;
      }

      /*圆点*/
      .dotline .dot {
        width: 12px;
        height: 12px;
        background: #ffffff;
        box-shadow: 0px 0px 6px 1px #00dcff, inset 0px 0px 5px 1px #00d5ff;
        border: 1px solid #00b3ff;
        position: absolute;
        top: 0px;
        left: 6px;
        border-radius: 50%;
      }
      .event_box {
        .time {
          width: 163px;
          height: 22px;
          line-height: 22px;
          font-size: 12px;
          color: #ffffff;
          background: url('@/assets/images/areatimebg.png') no-repeat
            center/100% 100%;
        }
        .content {
          width: 414px;
          min-height: 147px;
          margin-bottom: 10px;
          box-sizing: border-box;
          padding: 19px 0 8px;
          color: #ffffff;
          //background: url(@/assets/images/areaListBg.png) no-repeat center/100% 100%;
          background-size: cover;
          .address {
            height: 36px;
            line-height: 36px;
            background: url('@/assets/images/arealisttopBg.png') no-repeat
              center/100% 100%;
            img {
              margin-right: 5px;
            }
          }
          .bottom {
            width: 100%;
            box-sizing: border-box;
            background: url('@/assets/images/arealistbottom.png') no-repeat -1px/100%
              100%;
            padding: 16px 12px;
          }
          .early {
            font-size: 14px;
            display: flex;
            justify-content: space-around;
            .guoiconImg {
              width: 32px;
              height: 28px;
            }
            .warningcont {
              align-self: center;
              &.warningBlue {
                color: #569eff;
              }
              &.warningRed {
                color: #f22f2f;
              }
              &.warningOrange {
                color: #ff9405;
              }
              &.warningYellow {
                color: #f2de2f;
              }
            }
          }
          .prompt {
            text-align: left;
            margin-top: 11px;
            background: rgba(0, 116, 159, 0.5);
            border-radius: 6px 6px 6px 6px;
            padding: 6px;
            line-height: 22px;
          }
        }
      }
    }
    ul li {
      width: 100%;
      height: 60px;
      box-sizing: border-box;
      padding: 0 20px;
      display: flex;
      align-items: center;
      justify-content: center;
      border-bottom: 1px solid #757575;

      img {
        width: 44px;
        height: 38px;
        margin-right: 10px;
      }

      p {
        color: #fff;
        font-size: 14px;
        width: calc(100% - 54px);
        white-space: wrap;
      }
    }
    .no-warning {
      padding: 120px;
      display: flex;
      align-items: center;
      justify-content: center;
      span {
        font-size: 20px;
        color: gray;
        font-weight: bold;
        white-space: no-wrap;
      }
    }
  }
  .downloadbox {
    margin-top: 10px;
    .title {
      height: 32px;
      line-height: 32px;
      font-size: 14px;
      color: #ffffff;
      text-align: left;
      padding-left: 13px;
      background: url('@/assets/images/titleBg.png') no-repeat 3px;
    }
    .listbox {
      height: 58px;
      line-height: 58px;
      font-size: 14px;
      color: #ffffff;
      text-align: left;
      background: url('@/assets/images/downloadBg.png') no-repeat center;
      .imgicon {
        width: 39px;
        height: 39px;
        vertical-align: middle;
        margin: 0 12px 0 47px;
      }
      .listdown {
        display: inline-block;
        width: 108px;
        text-align: center;
        height: 36px;
        line-height: 36px;
        margin-left: 42px;
        cursor: pointer;
        background: url('@/assets/images/downbtnBg.png') no-repeat center;
      }
    }
    :deep(.el-input__wrapper) {
      height: 38px;
      color: #ffffff !important;
      background: rgba(2, 19, 43, 0.69) !important;
      box-shadow: inset 0px 0px 6px 1px #00b9ff;
      border-radius: 4px 4px 4px 4px;
      border: 1px solid rgba(34, 249, 255, 0.23) !important;
      margin: 12px 0;
      .el-range-separator,
      .el-range-input {
        color: #fff !important;
      }
    }
  }
}
@keyframes flicker {
  0% {
    opacity: 0;
  }
  50% {
    opacity: 0.5;
  }
  100% {
    opacity: 1;
  }
}
</style>
